import React, { Component } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { orderStyle, commonStyle } from '../_common/style';
import navigationSeevice from '../../../../components/navigation/navigationSeevice';
import { ProgressModal } from '../../../../components/common/ProgressModal';
import moment from 'moment';
import { CURRENCY } from '../../../../utils/helper';
import HTTP from '../../../../utils/http';
import { API, API_GENERATE } from '../../../../config/api';

/*
	data,
	type PAY->付款 JS->结算
*/



export class PayItemCpt extends Component {
	constructor(props) {
		super(props);
		this.state = {

		}
		this.lightBoxData = {
			modalVisible: false,
			onClosePress: this._closeLightBox,
			title: '',
			rowDataList: [],
			status: 'data',
			refreshIcon: false,
		}

	}


	/**
		* 付款申请状态
		* 30.审批中
		* 40.审核通过
		* 50.驳回
		* 65.付款中
		* 70.已付款
		*/
	_findApproval = (statusKey) => {
		if (statusKey == '30' || statusKey == '50' || statusKey == '40') return true;
		return false;
	}

	_getCheckApprove = async (data) => {
		//查询审批节点AUDIT_QUERY_COMPANY
		let params = {
			bizId: data.paymentApplyId,
			bizObjType: '13',
			processInstanceId: data.processInstanceId,
		}
		let responseData1 = await HTTP.GET(API_GENERATE(API.AUDIT_QUERY_COMPANY), params)
		responseData1 = responseData1 && responseData1.map((item) => {
			if (item.auditResult == 9) {
				item.auditResultName = '操作';
			}
			return item;
		})
		this.lightBoxData.modalVisible = true;
		this.lightBoxData.title = '审批流';
		this.lightBoxData.rowDataList = responseData1;
		this.setState({ ls: true })
	}

	_closeLightBox = () => {
		this.lightBoxData.modalVisible = false;
		this.setState({ ls: true });
	}

	// 付款模块
	_renderPay = (data,isTop0) => {
		let { itemData, flagData } = data || {};
		let { orderType } = flagData || {};
		let {
			paymentCode, paymentApplySourceStr, paymentStatusStr,
			receiptCode, receiptSourceStr, receiptStatusStr, currency,
			capitalAmount, receiptDate, remark, createTime, paymentDate, paymentApplyStatusStr, paymentApplyStatus,
			paymentApplyCode
		} = itemData || {};

		let changeData = {
			code: orderType == 'S' ? receiptCode : paymentCode, // 单号
			title: orderType == 'S' ? receiptSourceStr : paymentApplySourceStr, // 标题
			amount: orderType == 'S' ? capitalAmount : capitalAmount, // 金额
			time: orderType == 'S' ? receiptDate : paymentDate, // 交易日期
			remark,
			createTime: orderType == 'S' ? createTime : createTime,
			name: orderType == 'S' ? receiptStatusStr : paymentStatusStr
		}
		let isFind = this._findApproval(paymentApplyStatus);
		if (orderType == 'P' && isFind) {
			changeData.code = paymentApplyCode;
		}
		return (
			<View style={[orderStyle.listItem, isTop0 ? { marginTop: 0 } : null]}>
				<Text style={orderStyle.listTime}>{changeData.createTime}</Text>
				<View style={[orderStyle.listBody]}>
					<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
						<View style={[orderStyle.listHead, commonStyle.row2]}>
							<Text style={orderStyle.listTitle}>{orderType == 'S' ? '收款' : (isFind ? '申请' : '付款')}单{changeData.code}</Text>
							<Text style={orderStyle.listStatus}>{changeData.name}</Text>
						</View>
						<TouchableOpacity onPress={() => { if (!isFind) { return false; } this._getCheckApprove(itemData) }}>
							<Text style={[orderStyle.listStatus, isFind ? orderStyle.loadColor : null]}>{paymentApplyStatusStr}</Text>
						</TouchableOpacity>
					</View>
					<View style={commonStyle.row}>
						<View style={orderStyle.listRow}>
							<Text style={[orderStyle.listLable]}>{changeData.title || ''}({CURRENCY.getUnit(currency).text})</Text>
							<Text style={[orderStyle.listValue]}>{changeData.amount || '-'}</Text>
						</View>
						<View style={orderStyle.listRow}>
							<Text style={[orderStyle.listLable]}>{orderType == 'S' ? '收款日期' : '付款日期'}</Text>
							<Text style={[orderStyle.listValue]}>{changeData.time && moment(changeData.time).format('YYYY/MM/DD') || '-'}</Text>
						</View>
					</View>
					<View style={[commonStyle.row3, orderStyle.listRemark]}>
						<Text style={[orderStyle.listLable, orderStyle.listLineHeight]}>备注: </Text>
						<Text style={[orderStyle.listValue2, orderStyle.listLineHeight]}>{changeData.remark || '-'}</Text>
					</View>

				</View>
			</View>
		)
	}

	// 结算模块
	_renderJs = (data) => {
		let { itemData, flagData } = data || {};
		let { settleId, settleStatusName, settledQuantity, settledAmount, createTime } = itemData || {};
		let { orderId, orderType } = flagData || {};
		return (
			<View style={orderStyle.listItem}>
				<Text style={orderStyle.listTime}>{createTime}</Text>
				<View style={[orderStyle.listBody]}>
					<View style={[orderStyle.listHead, commonStyle.row2]}>
						<Text style={orderStyle.listTitle}>结算单 {settleId}</Text>
						<Text style={orderStyle.listStatus}>{settleStatusName}</Text>
					</View>

					<View style={commonStyle.row}>
						<View style={orderStyle.listRow}>
							<Text style={[orderStyle.listLable]}>结算量</Text>
							<Text style={[orderStyle.listValue]}>{settledQuantity || '-'}吨</Text>
						</View>
						<View style={orderStyle.listRow}>
							<Text style={[orderStyle.listLable]}>结算总额</Text>
							<Text style={[orderStyle.listValue]}>{settledAmount || '-'}元</Text>
						</View>
						<View style={[orderStyle.listRow]}>
							<Text style={[orderStyle.listLoad]} onPress={() => { navigationSeevice.navigate('InvoiceDetailsPage', { orderId, orderType, settleId }) }}>[结算明细]</Text>
						</View>
					</View>

				</View>
			</View>
		)
	}

	//10.草稿 20.待审批 30.审批中 40.审核通过 50.驳回 60.作废 70.已付款 80.待部门审批 90.待财务审批
	_getCode = (code) => {
		let codeStr = '';
		switch (code) {
			case '10':
				codeStr = '草稿';
				break;
			case '20':
				codeStr = '待审批';
				break;
			case '30':
				codeStr = '审批中';
				break;
			case '40':
				codeStr = '审核通过';
				break;
			case '50':
				codeStr = '驳回';
				break;
			case '60':
				codeStr = '作废';
				break;
			case '70':
				codeStr = '已付款';
				break;
			case '80':
				codeStr = '待部门审批';
				break;
			case '90':
				codeStr = '待财务审批';
				break;
		}
		return codeStr
	}


	render() {
		let { data, type,isTop0 } = this.props || {};
		return (
			<View>
				{type == 'PAY' ? this._renderPay(data,isTop0) : this._renderJs(data)}
				<ProgressModal {...this.lightBoxData} />
			</View>
		)
	}
}